package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author 久睡成瘾
 * @date 2025-10-13
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 根据设备编号查询设备信息
     *
     * @param innerCode 设备信息
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineByInnerCode(String innerCode) {

        return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //生成随机编号
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        //根据设备类型关联查询该类型信息
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        Long maxCapacity = vmType.getChannelMaxCapacity();
        vendingMachine.setChannelMaxCapacity(maxCapacity);
        //根据设备点位关联查询该点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);

        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());

        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);

        //新增货道
        Long row = vmType.getVmRow();//货道行
        Long col = vmType.getVmCol();//货道列
        Long id = vendingMachine.getId();

        //全部货道数据
        List<Channel> channelList = new ArrayList<>();

        for (int i = 1; i <= row; i++) {
            //外层遍历行
            for (int j = 1; j <= col; j++) {
                //内层遍历列
                Channel channel = new Channel();
                //构造channelCode
                channel.setChannelCode(i + "-" + j);
                channel.setVmId(id);
                channel.setInnerCode(innerCode);
                channel.setMaxCapacity(maxCapacity);
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());

                //插入list
                channelList.add(channel);
            }
        }

        //将货道数据批量插入
        channelMapper.insertChannelBatch(channelList);

        return result;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        //修改设备数据一致性优化
        //根据设备类型关联查询该类型信息
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        Long maxCapacity = vmType.getChannelMaxCapacity();
        vendingMachine.setChannelMaxCapacity(maxCapacity);
        //根据设备点位关联查询该点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        vendingMachine.setAddr(node.getAddress());
        vendingMachine.setBusinessType(node.getBusinessType());
        vendingMachine.setRegionId(node.getRegionId());
        vendingMachine.setPartnerId(node.getPartnerId());

        vendingMachine.setUpdateTime(DateUtils.getNowDate());

        int result = vendingMachineMapper.updateVendingMachine(vendingMachine);

        //修改货道表中货道最大容量和更新时间
        Channel channel = new Channel();
        channel.setVmId(vendingMachine.getId());
        channel.setMaxCapacity(maxCapacity);
        channel.setUpdateTime(DateUtils.getNowDate());
        channelMapper.updateChannelByVmId(channel);

        return result;
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
